package api

import (
	"net/http"
	"gitee.com/DengAnbang/streamWeb/public"
	"fmt"
	"gitee.com/DengAnbang/goutils/update"
)

var Apis = make(map[string]public.AppHandleFuncErr, 10)

func GetPatternByFun(app public.AppHandleFuncErr) string {
	for k, v := range Apis {
		if fmt.Sprint(app) == fmt.Sprint(v) {
			return k
		}
	}
	return ""
}
func Run(port string) {
	mux := http.NewServeMux()
	Apis["app/test/upload"] = testUpload

	Apis["/"] = notFoundHandler
	Apis["app/user/create"] = CreateUser
	Apis["app/user/login"] = LoginUser
	Apis["app/video/play"] = videoPlay
	Apis["app/video/upload"] = videoUpload

	Apis["/web/login"] = WebLogin
	Apis["/web/home"] = WebHome

	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	for k, v := range Apis {
		mux.HandleFunc(k, public.AppHandleFunc(v))
	}
	update.UpgradeService(":"+port, mux)
}

func notFoundHandler(w http.ResponseWriter, r *http.Request) error {
	http.Redirect(w, r, GetPatternByFun(WebLogin), http.StatusFound)
	return nil
}
